﻿using UnityEngine;
using UnityEngine.UI;

public class GuageController : MonoBehaviour {

    public Text ValueText;
    public SpriteRenderer Pointer;

    public float progress = 0;
    public GaugeInfo gauge;

    private void Start()
    {
        OnValueChange(gauge.min);
    }

    public void OnValueChange(float value)
    {
        if (null == gauge)
        {
            return;
        }

        progress = value;

        if (progress > gauge.max) progress = gauge.max;
        if (progress < gauge.min) progress = gauge.min;
        
        ValueText.text = string.Format("{0:.00}", progress);

        float per = progress / (gauge.max - gauge.min);
        if (gauge.invert)
        {
            per = (gauge.max - progress) / (gauge.max - gauge.min);
        }

        float temp = per * (gauge.maxAngle - gauge.minAngle);

        //float angle = guage.minAngle + (guage.maxAngle - guage.minAngle) * per;
        float angle = 360 - (gauge.minAngle + temp) + 90;
        if (angle >= 360)
        {
            angle = angle - 360;
        }

        Pointer.transform.localRotation = Quaternion.Euler(0, 0, angle);
    }
}
